package com.toijng.weixintracker;

import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.util.Log;
import android.view.View;
import android.view.accessibility.AccessibilityManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.toijng.weixintracker.entity.Bag;
import com.toijng.weixintracker.entity.BagItem;
import com.toijng.weixintracker.utils.HongbaoClient;

import java.util.List;

public class MainActivity extends AppCompatActivity implements AccessibilityManager.AccessibilityStateChangeListener {

    //开关切换按钮
    private Button switchPlugin;

    private EditText editText;
    private Button editButton;
    private TextView textView;
    //AccessibilityService 管理
    private AccessibilityManager accessibilityManager;

    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        accessibilityManager = (AccessibilityManager) getSystemService(Context.ACCESSIBILITY_SERVICE);
        accessibilityManager.addAccessibilityStateChangeListener(this);
        updateServiceStatus();
    }




    public  void init()
    {
        sharedPreferences=getSharedPreferences("DATA",MODE_PRIVATE);

        textView=(TextView)findViewById(R.id.url_Tv);
        switchPlugin = (Button) findViewById(R.id.button_accessible);

        editText=(EditText) findViewById(R.id.edit_url_Et);
        editButton=(Button)findViewById(R.id.button_edit);



        String url=sharedPreferences.getString("URL","");
        if (!url.equals(""))
        {
            textView.setText("目前服务器地址为"+url);

            SpannableString ss = new SpannableString("已有服务器地址,如需更改请输入后确认地址");
            AbsoluteSizeSpan ass = new AbsoluteSizeSpan(8,true);
            ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            editText.setHint(new SpannedString(ss));


        }else
        {
            textView.setText("尚未设置服务器地址");
            editText.setHint(getString(R.string.edit_hint));
            editText.requestFocus();
            switchPlugin.setVisibility(View.INVISIBLE);
        }


        editButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String temp=editText.getText().toString().trim();
                if (!TextUtils.isEmpty(temp))
                {
                    sharedPreferences.edit().putString("URL",temp).apply();
                    textView.setText("目前服务器地址为"+temp);
                    switchPlugin.setVisibility(View.VISIBLE);
                }else
                {
                    Toast.makeText(MainActivity.this, getString(R.string.url_hint),
                            Toast.LENGTH_SHORT).show();
                }

            }
        });
        switchPlugin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent accessibleIntent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
                startActivity(accessibleIntent);
            }
        });
    }

    @Override
    public void onAccessibilityStateChanged(boolean enabled) {
        updateServiceStatus();
    }


    /**
     * 更新当前 HongbaoService 显示状态
     */
    private void updateServiceStatus() {
        if (isServiceEnabled()) {
            switchPlugin.setText(getString(R.string.close_service));
        } else {
            switchPlugin.setText(getString(R.string.start_service));
        }
    }

    /**
     * 获取 HongbaoService 是否启用状态
     *
     * @return
     */
    private boolean isServiceEnabled() {
        List<AccessibilityServiceInfo> accessibilityServices =
                accessibilityManager.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_GENERIC);
        for (AccessibilityServiceInfo info : accessibilityServices) {
            if (info.getId().equals(getPackageName() + "/.Service.HongbaoService")) {
                return true;
            }
        }
        return false;
    }

    @Override
    protected void onDestroy() {
        //移除监听服务
        accessibilityManager.removeAccessibilityStateChangeListener(this);
        super.onDestroy();
    }
}
